﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using QMSDAL;

namespace QuaManSys.Common
{
    public class Reminder
    {
        public static List<InnerQualityInfo> QueryInReminderList(string status, CurUser user)
        {
            List<InnerQualityInfo> list = new List<InnerQualityInfo>();
            switch (status)
            {
                case "登记":
                    if (Permission.JudgePermission(user, "1A", "A"))
                    {
                        list = QueryInnerReg("登记", user);
                    }
                    break;
                case "分发":
                    if (Permission.JudgePermission(user, "1B", "B0"))
                    {
                        list = QueryInnerDis("分发", user);
                    }
                    break;
                case "处理":
                    if (Permission.JudgePermission(user, "1B", "B1"))
                    {
                        list = QueryInnerPro("处理", user);
                    }
                    break;
                case "归档":
                    if (Permission.JudgePermission(user, "1B", "D1"))
                    {
                        list = QueryInnerFle("处理", user); //status传处理
                    }
                    break;
                default:
                    list = null;
                    break;
            }
            return list;
        }

        #region 内部处理流程提醒

        /// <summary>
        /// status= 处理
        /// </summary>
        /// <param name="status"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        private static List<InnerQualityInfo> QueryInnerFle(string status, CurUser user)
        {
            using (var entity = new QuaManSysEntities())
            {
                var queryId = ((from q in entity.InnerResponsibleDepart
                                select q.qualityInfoId).Distinct()).Except((
                            from p in entity.InnerResponsibleDepart
                            where p.processed == 0
                            select p.qualityInfoId).Distinct());

                var query = (from q in entity.InnerQualityInfo
                             join p in queryId
                             on q.qualityInfoId equals p
                             where q.qualityInfoId == p && q.processingStatus == status
                             select q).Distinct();
                List<InnerQualityInfo> list = query.ToList();
                return list;
            }
        }
        /// <summary>
        /// status为处理
        /// </summary>
        /// <param name="status"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        private static List<InnerQualityInfo> QueryInnerPro(string status, CurUser user)
        {
            using (var entity = new QuaManSysEntities())
            {
                var inInfoPro =
                    from c in entity.InnerQualityInfo
                    join o in entity.InnerResponsibleDepart on c.qualityInfoId equals o.qualityInfoId
                    where c.processingStatus == status && user.department == o.department
                        && o.processed == 0
                    select c;
                List<InnerQualityInfo> list = inInfoPro.ToList();
                return list;
            }
        }
        /// <summary>
        /// status为分发
        /// </summary>
        /// <param name="status"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        private static List<InnerQualityInfo> QueryInnerDis(string status, CurUser user)
        {
            using (var entity = new QuaManSysEntities())
            {
                var query = from q in entity.InnerQualityInfo
                            where q.processingStatus == status
                            select q;
                List<InnerQualityInfo> list = query.ToList();
                return list;
            }
        }

        /// <summary>
        /// status为登记
        /// </summary>
        /// <param name="status"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        private static List<InnerQualityInfo> QueryInnerReg(string status, CurUser user)
        {
            using (var context = new QuaManSysEntities())
            {
                var query =
                from c in context.InnerQualityInfo
                where c.processingStatus == status && c.registrantName == user.employeeId
                select c;
                List<InnerQualityInfo> list = query.ToList();
                return list;
            }
        }

        #endregion

        public static List<OuterQualityInfo> QueryOutReminderList(string status, CurUser user)
        {
            List<OuterQualityInfo> list = new List<OuterQualityInfo>();
            switch (status)
            {
                case "登记":
                    if (Permission.JudgePermission(user, "2A", "A"))
                    {
                        list = QueryOutReg("登记", user);
                    }
                    break;
                case "分发":
                    if (Permission.JudgePermission(user, "2B", "B0"))
                    {
                        list = QueryOutDis("分发", user);
                    }
                    break;
                case "处理":
                    if (Permission.JudgePermission(user, "2B", "B1"))
                    {
                        list = QueryOutPro("处理", user);
                    }
                    break;
                case "闭环":
                    if (Permission.JudgePermission(user, "2B", "D0"))
                    {
                        list = QueryOutCls("处理", user); //status传处理
                    }
                    break;
                case "归档":
                    if (Permission.JudgePermission(user, "2B", "D1"))
                    {
                        list = QueryOutFle("闭环", user); //status传处理
                    }
                    break;
                default:
                    list = null;
                    break;
            }
            return list;
        }
#region 外部处理流程提醒

        private static List<OuterQualityInfo> QueryOutFle(string status, CurUser user)
        {
            using (var entity = new QuaManSysEntities())
            {
                var query = from q in entity.OuterQualityInfo
                            where q.processingStatus == status
                            select q;
                List<OuterQualityInfo> lstInfo = query.ToList();
                return lstInfo;
            }
        }

        private static List<OuterQualityInfo> QueryOutCls(string status, CurUser user)
        {
            using (var entity = new QuaManSysEntities())
            {
                var queryId = (from q in entity.OuterResponsibleDepart
                               select q.qualityInfoId).Distinct().Except((
                              from p in entity.OuterResponsibleDepart
                              where p.processed == 0
                              select p.qualityInfoId).Distinct());
                var query = from q in entity.OuterQualityInfo
                            join p in queryId
                            on q.qualityInfoId equals p
                            where q.qualityInfoId == p && q.processingStatus == status
                            select q;
                List<OuterQualityInfo> lstInfo = query.ToList();
                return lstInfo;
            }
            
        }

        private static List<OuterQualityInfo> QueryOutPro(string status, CurUser user)
        {
            using (var entity = new QuaManSysEntities())
            {
                var outInfoPro =
                    from c in entity.OuterQualityInfo
                    join o in entity.OuterResponsibleDepart on c.qualityInfoId equals o.qualityInfoId
                    where c.processingStatus == status && user.department == o.department && o.processed == 0
                    select c;
                List<OuterQualityInfo> list = outInfoPro.ToList();
                return list;
            }
        }

        private static List<OuterQualityInfo> QueryOutDis(string status, CurUser user)
        {
            using (var entity = new QuaManSysEntities())
            {
                var query = from q in entity.OuterQualityInfo
                            where q.processingStatus == status
                            select q;
                List<OuterQualityInfo> lstInfo = query.ToList();
                return lstInfo;
            }
        }

        private static List<OuterQualityInfo> QueryOutReg(string status, CurUser user)
        {
            using (var entity = new QuaManSysEntities())
            {
                var infoPro =
                    from c in entity.OuterQualityInfo
                    where c.processingStatus == "登记" && c.registerName == user.employeeId
                    select c;
                List<OuterQualityInfo> list = infoPro.ToList();
                return list;
            }
        }
#endregion
    }
}